package com.example.lib.extend

import android.content.Context
import android.graphics.Outline
import android.graphics.Typeface
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.widget.EditText
import android.widget.TextView
import androidx.core.view.marginBottom
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import androidx.core.view.marginTop
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.example.lib.R
import com.example.lib.listener.OnDoubleClickListener
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView

fun View.setOnDoubleClickListener(click: (view: View?) -> Unit) {
    setOnClickListener(object : OnDoubleClickListener() {
        override fun onDoubleClick(view: View?) {
            click.invoke(view)
        }
    })
}

fun TextView.cleanDrawable() {
    setCompoundDrawables(null, null, null, null)
}

fun EditText.stringText(): String {
    return text.toString()
}

fun TextView.stringText(): String {
    return text.toString()
}

fun EditText.cleanText() {
    setText("")
}

fun View.forceVisi() {
    visibility = View.VISIBLE
}

fun View.visi() {
    if (visibility != View.VISIBLE) visibility = View.VISIBLE
}

fun View.gone() {
    if (visibility != View.GONE) visibility = View.GONE
}

fun View.forceGone() {
    visibility = View.GONE
}

fun View.inVisi() {
    if (visibility != View.INVISIBLE) visibility = View.INVISIBLE
}

fun View.forceInVisi() {
    visibility = View.INVISIBLE
}

fun View.isVisi(flag: Boolean) {
    if (flag) visi() else gone()
}

fun View.isForceVisi(flag: Boolean) {
    if (flag) forceVisi() else forceGone()
}

fun View.isInVisi(flag: Boolean) {
    if (flag) visi() else inVisi()
}

fun View.isForceInVisi(flag: Boolean) {
    if (flag) forceVisi() else forceInVisi()
}

fun View.getVisi(): Boolean {
    return visibility == View.VISIBLE
}

/**
 * 动态设置高度
 * @param width
 *         宽度
 * @param height
 *         高度
 */
fun View?.setWidthOrHeight(width: Int = 0, height: Int = 0) {
    this?.let {
        if (height != 0) {
            if (height < 0) layoutParams.height = height
            else layoutParams.height = context.dip2px(height.toFloat())
        }
        if (width != 0) {
            if (width < 0) layoutParams.width = width
            else layoutParams.width = context.dip2px(width.toFloat())
        }
        layoutParams = layoutParams
    }
}

fun View?.setWidthOrHeightNot2Px(width: Int = 0, height: Int = 0) {
    this?.let {
        if (height != 0) {
            layoutParams.height = height
        }
        if (width != 0) {
            layoutParams.width = width
        }
        layoutParams = layoutParams
    }
}

fun View.setMargin(left: Int = marginLeft, top: Int = marginTop, right: Int = marginRight, bottom: Int = marginBottom) {
    (layoutParams as ViewGroup.MarginLayoutParams).setMargins(left, top, right, bottom)
    requestLayout()
}

fun View.setRadius(radius: Float) {
    outlineProvider = object : ViewOutlineProvider() {
        override fun getOutline(view: View, outline: Outline) {
            outline.setRoundRect(0, 0, view.width, view.height, context.dip2px(radius).toFloat())
        }
    }
    clipToOutline = true
}

fun ViewGroup.layoutParamsMatchParent() {
    val params = layoutParams
    params.width = ViewGroup.LayoutParams.MATCH_PARENT
    params.height = ViewGroup.LayoutParams.MATCH_PARENT
    layoutParams = params
}


//垂直列表
fun RecyclerView.verticalManager(): LinearLayoutManager {
    return LinearLayoutManager(context).let {
        layoutManager = it
        it
    }
}

//垂直列表
fun RecyclerView.verticalManager(success: (manager: LinearLayoutManager) -> Unit) {
    layoutManager = LinearLayoutManager(context).apply {
        success.invoke(this)
    }
}

//水平列表
fun RecyclerView.horizontalManager(isBanScroll: Boolean = false): LinearLayoutManager {
    val manager = object : LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) {
        override fun canScrollHorizontally(): Boolean {
            return !isBanScroll
        }
    }

    layoutManager = manager
    return manager
}

//网格列表
fun RecyclerView.gridManager(spanCount: Int, @RecyclerView.Orientation orientation: Int = RecyclerView.VERTICAL) {
    layoutManager = GridLayoutManager(context, spanCount, orientation, false)
}

fun ViewPagerHelper.bindViewPage2(magicIndicator: MagicIndicator, viewPager: ViewPager2) {

    viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrollStateChanged(state: Int) {
            magicIndicator.onPageScrollStateChanged(state)
        }

        override fun onPageSelected(position: Int) {
            magicIndicator.onPageSelected(position)
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels)
        }
    })
}

/**
 * 初始化
 */
fun MagicIndicator.initTab(
    pager: ViewPager2? = null,
    data: MutableList<String>?,
    normalColor: Int = R.color.black_45,
    chooseColor: Int = R.color.black,
    indicatorColor: Int = R.color.blue_4279ff,
    indicatorWidth: Float = 26f,
    indicatorHeight: Float = 2f,
    isAdjustMode: Boolean = true,
    click: (position: Int) -> Unit = {},
    banAutoSwitch: Boolean = false,
    scrollPivotX: Float = 0.9f,
    roundRadius: Float = 0f,
) {
    val commonNavigator = CommonNavigator(context)
    commonNavigator.isAdjustMode = isAdjustMode
    commonNavigator.scrollPivotX = scrollPivotX

    commonNavigator.adapter = object : CommonNavigatorAdapter() {
        override fun getCount(): Int {
            return if (data.isNullOrEmpty()) 0 else data.size
        }

        override fun getTitleView(context: Context, index: Int): IPagerTitleView {
            val simplePagerTitleView = ColorTransitionPagerTitleView(context)
            simplePagerTitleView.normalColor = context.getResourceColor(normalColor)
            simplePagerTitleView.selectedColor = context.getResourceColor(chooseColor)
            simplePagerTitleView.text = data?.get(index) ?: ""
            simplePagerTitleView.setTypeface(null, Typeface.BOLD)
            simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.sp2px(16).toFloat())
            simplePagerTitleView.setOnClickListener {
                if (pager == null || banAutoSwitch) click.invoke(index)
                else {
                    pager.currentItem = index
                }
            }
            return simplePagerTitleView
        }

        override fun getIndicator(context: Context): IPagerIndicator {
            val indicator = LinePagerIndicator(context)
            indicator.mode = LinePagerIndicator.MODE_EXACTLY
            indicator.yOffset = 30f
            indicator.roundRadius = context.dip2px(roundRadius).toFloat()
            indicator.lineWidth = context.dip2px(indicatorWidth).toFloat()
            indicator.setColors(context.getResourceColor(indicatorColor))
            indicator.lineHeight = context.dip2px(indicatorHeight).toFloat()
            return indicator
        }
    }

    navigator = commonNavigator
    pager?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrollStateChanged(state: Int) {
            this@initTab.onPageScrollStateChanged(state)
        }

        override fun onPageSelected(position: Int) {
            this@initTab.onPageSelected(position)
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            this@initTab.onPageScrolled(position, positionOffset, positionOffsetPixels)
        }
    })
}


